package com.ruoyi.ams.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.google.common.collect.Lists;

import com.ruoyi.ams.domain.vo.*;
import com.ruoyi.ams.service.AmsReusingService;
import com.ruoyi.ams.service.IAmsAssetInfoService;
import com.ruoyi.ams.service.IAmsAssetTypeService;
import com.ruoyi.ams.service.IAssetInfoViewService;
import com.ruoyi.asms.service.IAsmsAssetFullInfoService;
import com.ruoyi.common.core.domain.AsmsAssetFullInfo;
import com.ruoyi.common.core.domain.AssetInfoView;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.AmsAssetType;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.security.annotation.PreAuthorize;
import com.ruoyi.security.service.TokensService;
import com.ruoyi.system.controller.SysUserController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 资产类型信息Controller ddd
 *
 * @author yfkj
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/ams/AmsAssetType")
public class AmsAssetTypeController extends BaseController {
    @Autowired
    private IAmsAssetTypeService amsAssetTypeService;
    @Autowired
    private IAsmsAssetFullInfoService assetFullInfoService;
    @Autowired
    private IAssetInfoViewService assetInfoViewService;
    @Autowired
    private IAmsAssetInfoService amsAssetInfoService;
    @Autowired
    private TokensService tokenService;
    @Autowired
    private AmsReusingService amsReusingService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private SysUserController userController;

    @GetMapping("/testGetUser")
    public AjaxResult  testGetUser(){

        AjaxResult userTest = userController.getUserTest();
        return userTest;
    }

    /**
     * 查询资产类型信息列表（树结构）
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:list")
    @GetMapping("/list")
    @ApiOperation("查询资产类型信息列表（树结构）")
    public TableDataInfo list(AssetTypeListVo vo) {
        AmsAssetType amsAssetType = new AmsAssetType();
        BeanUtils.copyProperties(vo, amsAssetType);
        startPage();
        List<AmsAssetType> list = amsAssetTypeService.selectAmsAssetTypeList(amsAssetType);
        List<AmsAssetType> treeList = amsAssetTypeService.buildTree(list);
        return getDataTable(treeList);
    }

    @GetMapping("/getAllList")
    public R<List<AmsAssetType>> getAllList() {
        List<AmsAssetType> types = amsAssetTypeService.list();
        return R.ok(types);
    }

    /**
     * 查询下拉树表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:list")
    @GetMapping("/treeselect")
    @ApiOperation("查询类型下拉树表")
    public AjaxResult treeselect(@RequestParam(value = "资产类型名", required = false) String assetTypeName) {
        QueryWrapper<AmsAssetType> wrapper = new QueryWrapper<>();
        if (assetTypeName != null) {
            wrapper.like(AmsAssetType.ASSET_TYPE_NAME, assetTypeName);
        }
        List<TreeSelect> treeSelect = amsAssetTypeService.buildTreeSelect(amsAssetTypeService.list(wrapper));
        return AjaxResult.success(treeSelect);
    }
    /**
     * 查询下拉树表
     */

    @GetMapping("/wxtreeselect")
    public AjaxResult wxtreeselect(@RequestParam(value = "资产类型名", required = false) String assetTypeName) {
        QueryWrapper<AmsAssetType> wrapper = new QueryWrapper<>();
        if (assetTypeName != null) {
            wrapper.like(AmsAssetType.ASSET_TYPE_NAME, assetTypeName);
        }
        List<WxTreeSelect> treeSelect = amsAssetTypeService.buildWxTreeSelect(amsAssetTypeService.list(wrapper));
        return AjaxResult.success(treeSelect);
    }
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:list")
    @GetMapping("/rTreeselect")
    @ApiOperation("查询所有树表")
    public R<List<TreeSelect>> rTreeselect() {
        List<TreeSelect> treeSelects = amsAssetTypeService.buildTreeSelect(amsAssetTypeService.list(new QueryWrapper<AmsAssetType>()));
        System.out.println(treeSelects);
        return R.ok(treeSelects, "dddddd");
    }

    @GetMapping("/treeselectExclel")
    @ApiOperation("查询所有树表")
    public R<List<TreeSelect>> treeselectExclel() {
        List<TreeSelect> treeSelects = amsAssetTypeService.buildTreeSelectExcel(amsAssetTypeService.list(new QueryWrapper<AmsAssetType>()));
        System.out.println(treeSelects);
        return R.ok(treeSelects);
    }

    /**
     * 导出资产类型信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:export")
    @Log(title = "资产类型信息", businessType = BusinessType.EXPORT)
    @PostMapping(value="/export", produces = "application/octet-stream")
    @ApiOperation("导出资产类型信息列表")
    public void export(HttpServletResponse response, AmsAssetType amsAssetType) throws IOException {
        List<AmsAssetType> list = amsAssetTypeService.selectAmsAssetTypeList(amsAssetType);
        ExcelUtil<AmsAssetType> util = new ExcelUtil<AmsAssetType>(AmsAssetType.class);
        util.exportExcel(response, list, "资产类型信息数据");
    }

    /**
     * 获取资产类型信息详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:query")
    @GetMapping("/{assetTypeId}")
    @ApiOperation("获取资产类型信息详细信息,assetTypeId-资产类型ID-Long-必填")
    public R<AmsAssetType> getInfo(@PathVariable("assetTypeId") Long assetTypeId) {
        return R.ok(amsAssetTypeService.selectAmsAssetTypeByAssetTypeId(assetTypeId));
    }

    /**
     * 新增资产类型信息（同级）
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:add")
    @Log(title = "资产类型信息", businessType = BusinessType.INSERT)
    @PostMapping("/addSibling")
    @ApiOperation("新增资产类型信息（同级）")
    public AjaxResult addSibling(@RequestBody AssetTypeAddVo vo) {
        AmsAssetType amsAssetType = new AmsAssetType();
        BeanUtils.copyProperties(vo, amsAssetType);
        AmsAssetType typeName = amsAssetTypeService.getOne(new QueryWrapper<AmsAssetType>()
                .eq(AmsAssetType.ASSET_TYPE_NAME, vo.getAssetTypeName()), false);
        AmsAssetType typeCode = amsAssetTypeService.getOne(new QueryWrapper<AmsAssetType>()
                .eq(AmsAssetType.ASSET_TYPE_CODE, vo.getAssetTypeCode()), false);
        if (typeName != null) {
            return AjaxResult.error(vo.getAssetTypeName() + "已存在！");
        }
        if (typeCode != null) {
            return AjaxResult.error(vo.getAssetTypeCode() + "已存在！");
        }
        AmsAssetType type = amsAssetTypeService.selectAmsAssetTypeByAssetTypeId(vo.getAssetTypeId());
        if (type != null) {
            amsAssetType.setParentId(type.getParentId());
            amsAssetType.setAncestors(type.getAncestors());
        } else {
            //新增第一个资产类型
            amsAssetType.setParentId(0L);
            amsAssetType.setAncestors("0");
        }
        amsAssetType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(amsAssetTypeService.insertAmsAssetType(amsAssetType));
    }

    /**
     * 新增资产类型信息（下级）
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:add")
    @Log(title = "资产类型信息", businessType = BusinessType.INSERT)
    @PostMapping("/addLower")
    @ApiOperation("新增资产类型信息（下级）")
    public AjaxResult addLower(@RequestBody AssetTypeAddVo vo) {
        AmsAssetType amsAssetType = new AmsAssetType();
        BeanUtils.copyProperties(vo, amsAssetType);
        AmsAssetType typeName = amsAssetTypeService.getOne(new QueryWrapper<AmsAssetType>()
                .eq(AmsAssetType.ASSET_TYPE_NAME, vo.getAssetTypeName()), false);
        AmsAssetType typeCode = amsAssetTypeService.getOne(new QueryWrapper<AmsAssetType>()
                .eq(AmsAssetType.ASSET_TYPE_CODE, vo.getAssetTypeCode()), false);
        if (typeName != null) {
            return AjaxResult.error(vo.getAssetTypeName() + "已存在！");
        }
        if (typeCode != null) {
            return AjaxResult.error(vo.getAssetTypeCode() + "已存在！");
        }
        AmsAssetType typeParent = amsAssetTypeService.selectAmsAssetTypeByAssetTypeId(vo.getParentId());
        if (typeParent != null) {
            amsAssetType.setAncestors(typeParent.getAncestors() + "," + vo.getParentId());
        }
        amsAssetType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(amsAssetTypeService.insertAmsAssetType(amsAssetType));
    }

    /**
     * 修改资产类型信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:edit")
    @Log(title = "资产类型信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改资产类型信息")
    public AjaxResult edit(@RequestBody AssetTypeEditVo vo) {
        AmsAssetType amsAssetType = new AmsAssetType();
        BeanUtils.copyProperties(vo, amsAssetType);
        AmsAssetType parentType = amsAssetTypeService.selectAmsAssetTypeByAssetTypeId(vo.getParentId());
        if (parentType != null) {
            amsAssetType.setAncestors(parentType.getAncestors() + "," + vo.getParentId());
        }
        amsAssetType.setUpdateBy(SecurityUtils.getUsername());
        amsAssetTypeService.updateAmsAssetType(amsAssetType);

        //修改资产视图表中的相关数据
        assetFullInfoService.update(new UpdateWrapper<AsmsAssetFullInfo>()
                .set(AsmsAssetFullInfo.TYPE_PARENT_ID, vo.getParentId())
                .set(AsmsAssetFullInfo.ASSET_TYPE_ANCESTORS, amsAssetType.getAncestors())
                .set(AsmsAssetFullInfo.ASSET_TYPE_NAME, vo.getAssetTypeName())
                .set(AsmsAssetFullInfo.ASSET_TYPE_CODE, vo.getAssetTypeCode())
                .eq(AsmsAssetFullInfo.ASSET_TYPE_ID, vo.getAssetTypeId()));
        return AjaxResult.success("修改成功！");
    }

    /**
     * 删除资产类型信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "ams:AmsAssetType:remove")
    @Log(title = "资产类型信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{assetTypeId}")
    @ApiOperation("删除资产类型信息,assetTypeId-资产类型ID-Long-必填")
    public AjaxResult remove(@PathVariable Long assetTypeId) {
        return toAjax(amsAssetTypeService.deleteAmsAssetTypeByAssetTypeId(assetTypeId));
    }

    @GetMapping("/getAmsAssetTypeByIds")
    public R<Map<Long, AmsAssetType>> getAddressByIds(@RequestParam(value = "ids", required = true) Long[] ids) {
        Map<Long, AmsAssetType> map = new HashMap<>();
        if (ids == null || ids.length == 0) {
            return R.ok(map);
        }
        List<AmsAssetType> list = amsAssetTypeService.list(new QueryWrapper<AmsAssetType>().in(AmsAssetType.ASSET_TYPE_ID, Arrays.asList(ids)));

        for (AmsAssetType type : list) {
            map.put(type.getAssetTypeId(), type);
        }
        return R.ok(map);

    }

    @PreAuthorize
    @GetMapping("/getAssetTypeSummary")
    @ApiOperation("获取资产分类汇总表")
    public AjaxResult getAssetTypeSummary(AmsAssetType assetType) {
        Set<Long> userOwnDeptIds = tokenService.getUserOwnDeptIds();
        QueryWrapper<AssetInfoView> wrapper = new QueryWrapper<>();
        if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
            if (userOwnDeptIds != null && userOwnDeptIds.size() > 0) {
                wrapper.in(AssetInfoView.OWN_DEPT_ID, userOwnDeptIds);
            } else {
                return success();
            }
        }
        wrapper.select(AssetInfoView.ASSET_TYPE_ID);
        List<AssetInfoView> viewList = assetInfoViewService.list(wrapper);
        if (viewList.size() == 0) return AjaxResult.success(new ArrayList<AmsAssetType>());
        List<AmsAssetType> tree = amsReusingService.getAssetTypeSummary(viewList, assetType);
        return AjaxResult.success(tree);
    }

    @PreAuthorize
    @GetMapping("/treeSelectByTypeIds")
    @ApiOperation("查询下拉树表通过类型主键数组")
    public R<List<TreeSelect>> treeSelectByTypeIds(@RequestParam("typeIds") Long[] typeIds) {
        if (typeIds == null || typeIds.length <= 0) {
            return R.ok();
        }
        QueryWrapper<AmsAssetType> queryWrapper = new QueryWrapper<>();
        for (Long typeId : typeIds) {
            queryWrapper.like(AmsAssetType.ANCESTORS, "," + typeId + ",").or()
                    .likeLeft(AmsAssetType.ANCESTORS, "," + typeId).or()
                    .likeRight(AmsAssetType.ANCESTORS, typeId + ",")
                    .or()
                    .eq(AmsAssetType.ASSET_TYPE_ID, typeId).or();
        }
        List<AmsAssetType> list = amsAssetTypeService.list(queryWrapper);
        List<TreeSelect> treeSelects = amsAssetTypeService.buildTreeSelect(list);

        return R.ok(treeSelects);
    }


    /**
     * 该方法为解析国家标准分类汇总表并写入数据库
     *
     * @param file
     */
    @PostMapping("/import")
    public void importDataR(MultipartFile file) {
        ExcelUtil<TypeExcelVo> util = new ExcelUtil<TypeExcelVo>(TypeExcelVo.class);
        try {
            List<TypeExcelVo> list = util.importExcel(file.getInputStream());
            List<Long> parentIds = new ArrayList<>();
            parentIds.add(0L);
            List<String> ancestors = new ArrayList<>();
            ancestors.add("0");
            int lastKey = 0;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getAssetTypeName().contains("            ")) {

                    AmsAssetType type = new AmsAssetType();
                    type.setParentId(parentIds.get(parentIds.size() - 1));
                    type.setAncestors(ancestors.get(parentIds.size() - 1));
                    type.setAssetTypeCode(list.get(i).getAssetTypeCode());
                    type.setAssetTypeName(list.get(i).getAssetTypeName().replaceAll(" ", ""));
                    type.setCreateTime(System.currentTimeMillis());
                    type.setCreateBy("admin");
                    amsAssetTypeService.save(type);
                    if (list.get(i + 1).getAssetTypeName().contains("              ")) {
                        parentIds.add(type.getAssetTypeId());
                        ancestors.add(type.getAncestors() + "," + type.getAssetTypeId());
                    } else if (list.get(i + 1).getAssetTypeName().contains("            ")) {

                    } else {
                        parentIds.remove(parentIds.get(parentIds.size() - 1));
                        ancestors.remove(ancestors.get(ancestors.size() - 1));
                    }
                } else if (list.get(i).getAssetTypeName().contains("          ")) {
                    AmsAssetType type = new AmsAssetType();
                    type.setParentId(parentIds.get(parentIds.size() - 1));
                    type.setAncestors(ancestors.get(parentIds.size() - 1));
                    type.setAssetTypeCode(list.get(i).getAssetTypeCode());
                    type.setAssetTypeName(list.get(i).getAssetTypeName().replaceAll(" ", ""));
                    type.setCreateTime(System.currentTimeMillis());
                    type.setCreateBy("admin");
                    amsAssetTypeService.save(type);
                    if (list.get(i + 1).getAssetTypeName().contains("            ")) {
                        parentIds.add(type.getAssetTypeId());
                        ancestors.add(type.getAncestors() + "," + type.getAssetTypeId());
                    } else if (list.get(i + 1).getAssetTypeName().contains("          ")) {

                    } else {
                        parentIds.remove(parentIds.get(parentIds.size() - 1));
                        ancestors.remove(ancestors.get(ancestors.size() - 1));
                    }

                } else if (list.get(i).getAssetTypeName().contains("        ")) {

                    AmsAssetType type = new AmsAssetType();
                    type.setParentId(parentIds.get(parentIds.size() - 1));
                    type.setAncestors(ancestors.get(parentIds.size() - 1));
                    type.setAssetTypeCode(list.get(i).getAssetTypeCode());
                    type.setAssetTypeName(list.get(i).getAssetTypeName().replaceAll(" ", ""));
                    type.setCreateTime(System.currentTimeMillis());
                    type.setCreateBy("admin");
                    amsAssetTypeService.save(type);
                    if (list.get(i + 1).getAssetTypeName().contains("          ")) {
                        parentIds.add(type.getAssetTypeId());
                        ancestors.add(type.getAncestors() + "," + type.getAssetTypeId());
                    } else if (list.get(i + 1).getAssetTypeName().contains("        ")) {

                    } else {
                        parentIds.remove(parentIds.get(parentIds.size() - 1));
                        ancestors.remove(ancestors.get(ancestors.size() - 1));
                    }
                } else if (list.get(i).getAssetTypeName().contains("      ")) {

                    AmsAssetType type = new AmsAssetType();
                    type.setParentId(parentIds.get(parentIds.size() - 1));
                    type.setAncestors(ancestors.get(parentIds.size() - 1));
                    type.setAssetTypeCode(list.get(i).getAssetTypeCode());
                    type.setAssetTypeName(list.get(i).getAssetTypeName().replaceAll(" ", ""));
                    type.setCreateTime(System.currentTimeMillis());
                    type.setCreateBy("admin");
                    amsAssetTypeService.save(type);
                    if (list.get(i + 1).getAssetTypeName().contains("        ")) {
                        parentIds.add(type.getAssetTypeId());
                        ancestors.add(type.getAncestors() + "," + type.getAssetTypeId());
                    } else if (list.get(i + 1).getAssetTypeName().contains("      ")) {

                    } else {
                        parentIds.remove(parentIds.get(parentIds.size() - 1));
                        ancestors.remove(ancestors.get(ancestors.size() - 1));
                    }
                } else if (list.get(i).getAssetTypeName().contains("    ")) {
                    AmsAssetType type = new AmsAssetType();
                    type.setParentId(parentIds.get(parentIds.size() - 1));
                    type.setAncestors(ancestors.get(parentIds.size() - 1));
                    type.setAssetTypeCode(list.get(i).getAssetTypeCode());
                    type.setAssetTypeName(list.get(i).getAssetTypeName().replaceAll(" ", ""));
                    type.setCreateTime(System.currentTimeMillis());
                    type.setCreateBy("admin");
                    amsAssetTypeService.save(type);
                    if (list.get(i + 1).getAssetTypeName().contains("      ")) {
                        parentIds.add(type.getAssetTypeId());
                        ancestors.add(type.getAncestors() + "," + type.getAssetTypeId());
                    } else if (list.get(i + 1).getAssetTypeName().contains("    ")) {

                    } else {
                        parentIds.remove(parentIds.get(parentIds.size() - 1));
                        ancestors.remove(ancestors.get(ancestors.size() - 1));
                    }

                } else if (list.get(i).getAssetTypeName().contains("  ")) {
                    AmsAssetType type = new AmsAssetType();
                    type.setParentId(parentIds.get(parentIds.size() - 1));
                    type.setAncestors(ancestors.get(parentIds.size() - 1));
                    type.setAssetTypeCode(list.get(i).getAssetTypeCode());
                    type.setAssetTypeName(list.get(i).getAssetTypeName().replaceAll(" ", ""));
                    type.setCreateTime(System.currentTimeMillis());
                    type.setCreateBy("admin");
                    amsAssetTypeService.save(type);
                    if (list.get(i + 1).getAssetTypeName().contains("    ")) {
                        parentIds.add(type.getAssetTypeId());
                        ancestors.add(type.getAncestors() + "," + type.getAssetTypeId());
                    } else if (list.get(i + 1).getAssetTypeName().contains("  ")) {

                    } else {
                        parentIds.remove(parentIds.get(parentIds.size() - 1));
                        ancestors.remove(ancestors.get(ancestors.size() - 1));
                    }
                } else {
                    AmsAssetType one = amsAssetTypeService.getOne(new QueryWrapper<AmsAssetType>().eq(AmsAssetType.ASSET_TYPE_NAME, list.get(i).getZcdl()).eq(AmsAssetType.PARENT_ID, 0));

                    AmsAssetType type = new AmsAssetType();
                    type.setParentId(one.getAssetTypeId());
                    type.setAncestors(one.getAncestors() + "," + one.getAssetTypeId());
                    type.setAssetTypeCode(list.get(i).getAssetTypeCode());
                    type.setAssetTypeName(list.get(i).getAssetTypeName().replaceAll(" ", ""));
                    type.setCreateTime(System.currentTimeMillis());
                    type.setCreateBy("admin");
                    amsAssetTypeService.save(type);
                    if (list.get(i + 1).getAssetTypeName().contains("  ")) {
                        parentIds.add(type.getAssetTypeId());
                        ancestors.add(type.getAncestors() + "," + type.getAssetTypeId());
                    }
                }
            }

        } catch (Exception e) {

        }
    }

    /**
     * 根据类型名称获取资产类型ID
     *
     * @param typeName 资产类型名称
     * @return 资产类型ID集合
     */
    @GetMapping("/getAssetTypeByName")
    public R<HashSet<Long>> getAssetTypeByName(@RequestParam("typeName") String typeName) {
        HashSet<Long> typeIds = new HashSet<>();
        List<AmsAssetType> typeList = amsAssetTypeService.list(new QueryWrapper<AmsAssetType>()
                .like(AmsAssetType.ASSET_TYPE_NAME, typeName));
        if (typeList != null) {
            typeList.forEach(type -> typeIds.add(type.getAssetTypeId()));
        }
        return R.ok(typeIds);
    }

    /**
     *
     *
     * @return
     */
    @GetMapping("/getLowAssetType")
    public AjaxResult getLowAssetType() {
        List<AmsAssetType> lowAssetTypeList = redisService.getCacheList("lowAssetTypeList");
        if (redisService.getCacheList("lowAssetTypeList").size() == 0) {
            List<AmsAssetType> resultList = new ArrayList<>();
            List<AmsAssetType> list = amsAssetTypeService.list(new QueryWrapper<AmsAssetType>()
                    .like(AmsAssetType.ANCESTORS, "0,2,"));
            for (AmsAssetType type : list) {
                List<AmsAssetType> list1 = amsAssetTypeService.list(new QueryWrapper<AmsAssetType>()
                        .eq(AmsAssetType.PARENT_ID, type.getAssetTypeId()));
                if (list1 != null && list1.size() > 0) continue;
                resultList.add(type);
            }
            redisService.setCacheList("lowAssetTypeList", resultList);
            return AjaxResult.success(resultList);
        }
        return AjaxResult.success(redisService.getCacheList("lowAssetTypeList"));
    }

    /**
     * 获取资产类型的的所有叶子节点
     *
     * @return
     */
    @GetMapping("/getSonList")
    public AjaxResult getSonList(HttpServletResponse response) throws IOException {
        List<AmsAssetType> types =new ArrayList<>();
       
        List<AmsAssetType> allTypes =amsAssetTypeService.list();
        Map<Long, AmsAssetType> idsMap = allTypes.stream().collect(Collectors.toMap(AmsAssetType::getAssetTypeId, item->item));

        List<AmsAssetType> hasParentsList = amsAssetTypeService.list(new QueryWrapper<AmsAssetType>().in(AmsAssetType.PARENT_ID, idsMap.keySet()));

        Map<Long, List<AmsAssetType>> collect = new HashMap<>();
        for (AmsAssetType item : hasParentsList) {
            if (collect.get(item.getParentId()) != null) {
                List<AmsAssetType> typesHere= collect.get(item.getParentId());
                typesHere.add(item);
                collect.put(item.getParentId(),typesHere);
            }else {
                collect.put(item.getParentId(), Lists.newArrayList(item));

            }
        }
        Set<Long> ids = idsMap.keySet();
        ids.removeAll(collect.keySet());
        ids.forEach(id->types.add(idsMap.get(id)));
        ExcelUtil<AmsAssetType> util = new ExcelUtil<AmsAssetType>(AmsAssetType.class);
        util.exportExcel(response, types, "资产类型信息数据");
        
        return AjaxResult.success(types);
    }
}
